package com.jungle.web.controller.system;

import cn.dev33.satoken.annotation.SaIgnore;
import com.jungle.common.core.controller.BaseController;
import com.jungle.common.core.domain.OptionVo;
import com.jungle.common.core.domain.PageQuery;
import com.jungle.common.core.domain.R;
import com.jungle.common.core.page.TableDataInfo;
import com.jungle.common.utils.StringUtils;
import com.jungle.system.domain.CommonArea;
import com.jungle.system.domain.bo.CommonAreaBo;
import com.jungle.system.domain.vo.CommonAreaSelectVo;
import com.jungle.system.domain.vo.CommonAreaVo;
import com.jungle.system.service.ICommonAreaService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 地域
 *
 * @author caomin
 * @date 2023-02-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/area")
public class CommonAreaController extends BaseController {

    private final ICommonAreaService iCommonAreaService;

    /**
     * 查询地域列表
     */
    @GetMapping("/list")
    public TableDataInfo<CommonArea> list(CommonAreaBo bo, PageQuery pageQuery) {
        return iCommonAreaService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取地域详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<CommonAreaVo> getInfo(@NotNull(message = "主键不能为空")
                                   @PathVariable String id) {
        return R.ok(iCommonAreaService.queryById(id));
    }

    /**
     * 获取地域详所在位置
     */
    @SaIgnore
    @GetMapping("/local/{areaCode}")
    public R<List<String>> findLocal(@NotNull(message = "主键不能为空")
                                     @PathVariable String areaCode) {
        return R.ok(iCommonAreaService.findLocal(areaCode));
    }

    /**
     * 获取地域详细信息
     */
    @SaIgnore
    @GetMapping("/parent/{areaCode}")
    public R<List<CommonAreaSelectVo>> findByParent(@NotNull(message = "主键不能为空")
                                                    @PathVariable String areaCode) {
        return R.ok(iCommonAreaService.queryByParent(areaCode));
    }


    /**
     * 获取选择框列表
     */
    @GetMapping("/optionselect")
    public R<List<OptionVo>> optionSelect(String query, String ids, String field, PageQuery pageQuery) {
        String[] queryIds = {};
        if (StringUtils.isNotBlank(ids)) {
            queryIds = ids.split(",");
        }
        List<OptionVo> optionVos = iCommonAreaService.optionSelect(query, queryIds, field, pageQuery);
        return R.ok(optionVos);
    }
}
